<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class CommonController extends Controller
{
    /**
     * 上传文件接口
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function uploadFile(Request $request)
    {
        $data = [];
        if ($files = $request->file()) {

            $path = sprintf("/data/upload/%s/%s/%s", date('Y', time()), date('m', time()),
                date('d', time()));
            $filePath = base_path('public' . $path);
            foreach ($files as $file) {
                $fileName = \Illuminate\Support\Str::random(16) . '.' . $file->getClientOriginalExtension();
                $file->move($filePath, $fileName);
                $data = env('APP_URL') .'/'. $path .'/'. $fileName;
            }
            $return = [
                'code' => 0,
                'msg'   => '成功',
                'data' => $data
            ];
            return response()->json($return);
        }
        $data = [
            'code' => -1,
            'msg'   => '失败',
        ];
        return response()->json($data);

    }

    /**
     * 富文本编辑
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadFileLayEdit(Request $request){
        $data = [];
        if ($files = $request->file()) {

            $path = sprintf("/data/upload/%s/%s/%s", date('Y', time()), date('m', time()),
                date('d', time()));
            $filePath = base_path('public' . $path);
            foreach ($files as $file) {
                $fileName = \Illuminate\Support\Str::random(16) . '.' . $file->getClientOriginalExtension();
                $file->move($filePath, $fileName);
                $data = env('APP_URL') .'/'. $path .'/'. $fileName;
            }
            $return = [
                'code' => 0,
                'msg'   => '成功',
                'data' => ['src' => $data, 'title' => $data]
            ];
            return response()->json($return);
        }
        $data = [
            'code' => -1,
            'msg'   => '失败',
            'data' => ['src' => $data, 'title' => $data]
        ];
        return response()->json($data);
    }


    /**
     * 上传多张图片
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function uploadFileMany(Request $request)
    {

        if ($files = $request->file('file')) {
            $data = [];
            $path = sprintf("/data/upload/%s/%s/%s", date('Y', time()), date('m', time()),
                date('d', time()));
            $filePath = base_path('public' . $path);
            foreach ($files as $file) {
                $fileName = \Illuminate\Support\Str::random(16) . '.' . $file->getClientOriginalExtension();

                $file->move($filePath, $fileName);
                $data[] = env('apiUrl') .'/'. $path .'/'. $fileName;
            }
            return $this->renderJson($data);
        }
        return $this->error();
    }


    /**
     * 上传二进制图片
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function uploadFileFlow(Request $request)
    {
        $base64_image_content =$request->input('file');
        if(!$base64_image_content){
            $data = [
                'code' => -1,
                'msg'   => '失败',
            ];
            return response()->json($data);
        }
        $data = [];
        for($i = 0; $i< count($base64_image_content); $i++){
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content[$i], $result)){
                $type = $result[2];
                $path = sprintf("/data/upload/%s/%s/%s", date('Y', time()), date('m', time()),
                    date('d', time()));
                $filePath = public_path($path);

                if(!is_dir($filePath))
                {
                    mkdir($filePath,0700, true);
                }
                $fileName = time().".{$type}";
                $filePath = $filePath.'/'.$fileName;
                if (file_put_contents($filePath, base64_decode(str_replace($result[1], '', $base64_image_content[$i])))){
                    $data[] = env('apiUrl') .'/' . $path .'/'. $fileName;
                }else{
                    $data = [
                        'code' => -1,
                        'msg'   => '失败',
                    ];
                    return response()->json($data);
                }
            }
        }
        $data = [
            'code' => 0,
            'msg'   => '成功',
        ];
        return response()->json($data);

    }

}
